﻿using AutoMapper;
using DFGK.WMS.Common.Web;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using NPOI.POIFS.Storage;
using NPOI.XSSF.UserModel.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class PDASelectOutboundRepository : BaseRepository<TShippingOrderDetail>, IPDASelectOutboundRepository
    {
        private ITOutboundOrderDetailRepository TOutboundOrderDetail;

        public PDASelectOutboundRepository(DFGK_DB context, ITOutboundOrderDetailRepository TOutboundOrderDetail, IMapper mapper) : base(context, mapper)
        {
            this.TOutboundOrderDetail = TOutboundOrderDetail;
        }

        /// <summary>
        /// 拣选出库
        /// </summary>
        /// <param name="PalletBarcode"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool GetSelectShipping(string PalletBarcode, string ShippingOrderSn, string code,/*decimal OutQty,*/ out string msg)
        {
            using (var tran = Context.Database.BeginTransaction())
                try
                {
                    msg = "";
                    //查询是否有此托盘
                    var palletBarAll = Context.TPallets.Where(x => x.PalletBarcode == PalletBarcode).ToList();
                    if (palletBarAll.Count() <= 0)
                    {
                        msg = "未查询到此托盘";
                        tran.Rollback();
                        return false;
                    }
                    //查看上架单
                    var putobj = Context.TPutawayOrders.FirstOrDefault(x => x.PalletBarcode == PalletBarcode && x.BillType == 6 && x.Status == 4);
                    if (putobj != null)
                    {
                        msg = "该托盘正在拣选作业中";
                        return false;
                    }
                    //查询是否有拣选台
                    var stations = Context.TStations
                        .Where(x => x.FunctionType == 4 && x.Status == 1);
                    if (stations.Count() <= 0)
                    {
                        msg = "暂无可用拣选台";
                        tran.Rollback();
                        return false;
                    }
                    //根据托盘查找库存数据
                    var palletBarcodeInInventorys = Context.TInventories.Where(x => x.PalletBarcode == PalletBarcode && x.Status==4);
                    if (!palletBarcodeInInventorys.Any())
                    {
                        msg = "未找到库存数据";
                        tran.Rollback();
                        return false;
                    }
                    else
                    {
                        DateTime now = DateTime.Now;                                 
                        var cell = Context.TCells.Where(x => x.Code == palletBarcodeInInventorys.First().LocationCode).FirstOrDefault();
                        if(cell==null)
                        {
                            return false;
                        }
                        //回流任务添加上架单数据
                        TPutawayOrder putawayOrder = new TPutawayOrder()
                        {
                            Code = "PUT" + DateTime.Now.ToString("yyyyMMddHHmmssfff"),
                            //RelatedOrderDetailId = orderDetail.Id,
                            //StackingStorageId = stackingStorageId,
                            PalletBarcode = palletBarAll.First().PalletBarcode,
                            //ParentPalletBarcode = stackingStorage.ParentPalletBarcode,
                            LocationCode = cell.Code,
                            BillType = 6,
                            LocationDepth = cell.DepthIndex,
                            Status = 4,
                            Qty = palletBarcodeInInventorys.First().AvailableQty,
                            CreateDate = DateTime.Now,
                            MaterialId = palletBarcodeInInventorys.First().MaterialId,
                            MaterialCode = palletBarcodeInInventorys.First().MaterialCode,
                            MaterialName = palletBarcodeInInventorys.First().MaterialName,
                            //MaterialKeyId = orderDetail.MaterialKeyId,
                            BatchNo = palletBarcodeInInventorys.First().BatchNo,
                            SmallBatchNo = palletBarcodeInInventorys.First().SmallBatchNo,
                            OwnerId = palletBarcodeInInventorys.First().OwnerId,
                            OwnerName = palletBarcodeInInventorys.First().OwnerName,
                            CreateUserId = 0,
                            CreateUserName = "系统",
                            SourceType = 1,
                            //站点编码
                            SiteCode = code,
                            //巷道
                            Roadway = cell.RoutewayId.ToString(),
                        };
                        Context.TPutawayOrders.Add(putawayOrder);
                        if (Context.SaveChanges() <= 0)
                        {
                            msg = "上架单保存失败";
                            tran.Rollback();
                            return false;
                        }
                        //回流入库下发任务
                        //PutChooseDto dto = new PutChooseDto()
                        //{
                        //    taskCode = putawayOrder.Code,
                        //    SiteCode = putawayOrder.SiteCode,
                        //    Roadway = putawayOrder.Roadway
                        //};
                        //HttpHelper.SendPostRequest("入库拣选", dto);
                    }
                    #region 出库单
                    //根据托盘查询出库单
                    //var palletBar = palletBarAll.First();
                    //var shippingListAll = Context.TShippingLockCells
                    //    .Where(x => x.PalletBarcode == PalletBarcode && x.CellCode == palletBar.LocationCode)
                    //    .OrderByDescending(x => x.CreateDate)
                    //    .ToList();
                    //if (shippingListAll.Count() <= 0)
                    //    {
                    //    msg = "未查询到出库单相关数据";
                    //    tran.Rollback();
                    //    return false;
                    //}
                    //var shippingOrderAll = Context.TShippingOrderDetails.First(x => x.OrderSn == ShippingOrderSn);
                    //if (shippingOrderAll == null)
                    //{
                    //    msg = "此托盘暂无创建出库单";
                    //    tran.Rollback();
                    //    return false;
                    //}
                    //if(shippingOrderAll.Status == 4)
                    //{
                    //    msg = shippingOrderAll.OrderSn + "出库单已完成,暂时无法完成拣选";
                    //    tran.Rollback();
                    //    return false;
                    //}

                    ////查询库存表中此托盘有多少物料
                    //var palletBarcodeInInventory = Context.TInventories.Where(x => x.PalletBarcode == PalletBarcode).ToList();
                    //if (palletBarcodeInInventory.Count == 0)
                    //{
                    //    msg = "此托盘中不存在物料";
                    //    tran.Rollback();
                    //    return false;
                    //}

                    //else if (palletBarcodeInInventory.Count() == 1)
                    //{
                    //    if (palletBarcodeInInventory.First().MaterialCode != shippingListAll.First().MaterialCode)
                    //    {
                    //        msg = "此物料未创建出库单" + shippingListAll.First().MaterialCode;
                    //        tran.Rollback();
                    //        return false;
                    //    }
                    //    if (palletBarcodeInInventory.First().AvailableQty > shippingListAll.First().OutQty)
                    //    {
                    //        DateTime now = DateTime.Now;
                    //        string year = now.Year.ToString();
                    //        string month = now.Month.ToString().PadLeft(2, '0');
                    //        string day = now.Day.ToString().PadLeft(2, '0');
                    //        string hour = now.Hour.ToString().PadLeft(2, '0');
                    //        string minute = now.Minute.ToString().PadLeft(2, '0');
                    //        string second = now.Second.ToString().PadLeft(2, '0');
                    //        var cell = Context.TCells.Where(x => x.Code == palletBarAll.First().LocationCode).FirstOrDefault();
                    //        //回流任务添加上架单数据
                    //        TPutawayOrder putawayOrder = new TPutawayOrder()
                    //        {
                    //            Code = "R" + $"{year}{month}{day}{hour}{minute}{second}",
                    //            //RelatedOrderDetailId = orderDetail.Id,
                    //            //StackingStorageId = stackingStorageId,
                    //            PalletBarcode = palletBarAll.First().PalletBarcode,
                    //            //ParentPalletBarcode = stackingStorage.ParentPalletBarcode,
                    //            LocationCode = cell.Code,
                    //            BillType=6,
                    //            LocationDepth = cell.DepthIndex,
                    //            Status = 4,
                    //            Qty = palletBarcodeInInventory.First().AvailableQty - shippingListAll.First().OutQty,
                    //            CreateDate = DateTime.Now,
                    //            MaterialId = shippingOrderAll.MaterialId,
                    //            MaterialCode = shippingOrderAll.MaterialCode,
                    //            MaterialName = shippingOrderAll.MaterialName,
                    //            //MaterialKeyId = orderDetail.MaterialKeyId,
                    //            BatchNo = shippingOrderAll.BatchNo,
                    //            SmallBatchNo = shippingOrderAll.SmallBatchNo,
                    //            OwnerId = shippingOrderAll.OwnerId,
                    //            OwnerName = shippingOrderAll.OwnerName,
                    //            CreateUserId = 0,
                    //            CreateUserName = "系统",
                    //            SourceType = 1,
                    //            //站点编码
                    //            SiteCode = stations.First().Code,
                    //            //巷道
                    //            Roadway = cell.RoutewayId.ToString(),
                    //        };
                    //        Context.TPutawayOrders.Add(putawayOrder);
                    //        ////回流调动堆垛机
                    //        //TaskLiku task = new TaskLiku()
                    //        //{
                    //        //    taskId = shippingListAll.First().OrdeSn,
                    //        //    taskSubType = "IN",
                    //        //    startPositionId = stations.First().Code,
                    //        //    endPositionId = palletBarAll.First().LocationCode,
                    //        //    palletCode = palletBarAll.First().PalletBarcode
                    //        //};
                    //        //HttpHelper.WCSSendPostRequest("Api/ApiWCS/ReceiveTask", task);

                    //        //创建回流入库单
                    //        //TReceivingOrderDetail receivingOrderDetail = new TReceivingOrderDetail();
                    //        //DateTime now = DateTime.Now;
                    //        //string year = now.Year.ToString();
                    //        //string month = now.Month.ToString().PadLeft(2, '0');
                    //        //string day = now.Day.ToString().PadLeft(2, '0');
                    //        //string hour = now.Hour.ToString().PadLeft(2, '0');
                    //        //string minute = now.Minute.ToString().PadLeft(2, '0');
                    //        //string second = now.Second.ToString().PadLeft(2, '0');
                    //        //receivingOrderDetail.OrderSn = "R" + $"{year}{month}{day}{hour}{minute}{second}";
                    //        //receivingOrderDetail.BillType = 6;
                    //        //receivingOrderDetail.WarehouseCode = shippingOrderAll.WarehouseCode;
                    //        //var warehouseList = Context.TWarehouses.Where(x => x.Code == shippingOrderAll.WarehouseCode);
                    //        //if (warehouseList.Count() <= 0)
                    //        //{
                    //        //    msg = "未查询到库房";
                    //        //    tran.Rollback();
                    //        //    return false;
                    //        //}
                    //        //receivingOrderDetail.WarehouseName = warehouseList.First().Name;
                    //        //receivingOrderDetail.MaterialId = shippingOrderAll.MaterialId;
                    //        //receivingOrderDetail.MaterialCode = shippingOrderAll.MaterialCode;
                    //        //receivingOrderDetail.MaterialName = shippingOrderAll.MaterialName;
                    //        //receivingOrderDetail.MaterialTypeCode = shippingOrderAll.MaterialTypeCode;
                    //        //receivingOrderDetail.AreaCode = shippingOrderAll.AreaCode;
                    //        //receivingOrderDetail.AreaName = shippingOrderAll.AreaName;
                    //        //receivingOrderDetail.PalletBarcode = PalletBarcode;
                    //        //receivingOrderDetail.Qty = palletBarcodeInInventory.First().AvailableQty - shippingListAll.First().OutQty;
                    //        //receivingOrderDetail.ReceivedQty = palletBarcodeInInventory.First().AvailableQty - shippingListAll.First().OutQty;
                    //        //receivingOrderDetail.UnitId = shippingOrderAll.UnitId;
                    //        //receivingOrderDetail.UnitName = shippingOrderAll.UnitName;
                    //        //receivingOrderDetail.Erpcode = shippingOrderAll.Erpcode;
                    //        //receivingOrderDetail.BatchNo = shippingOrderAll.BatchNo;
                    //        //receivingOrderDetail.SmallBatchNo = shippingOrderAll.SmallBatchNo;
                    //        //receivingOrderDetail.OwnerId = shippingOrderAll.OwnerId;
                    //        //receivingOrderDetail.OwnerName = shippingOrderAll.OwnerName;
                    //        //receivingOrderDetail.Status = 2;
                    //        //receivingOrderDetail.LocationId = palletBarcodeInInventory.First().LocationId;
                    //        //receivingOrderDetail.LocationName = palletBarcodeInInventory.First().LocationName;
                    //        //receivingOrderDetail.LocationCode = palletBarcodeInInventory.First().LocationCode;
                    //        //receivingOrderDetail.CreateDate = DateTime.Now;
                    //        //receivingOrderDetail.MaterialName = shippingOrderAll.MaterialName;
                    //        //receivingOrderDetail.Status = 2;
                    //        //Context.TReceivingOrderDetails.Add(receivingOrderDetail);

                    //        //decimal? returnQty = palletBarcodeInInventory.First().AvailableQty - shippingListAll.First().OutQty;
                    //        //msg = "物料:" + shippingListAll.First().MaterialName + "回流" + returnQty;
                    //        //return false;

                    //    }
                    //    else if (palletBarcodeInInventory.First().AvailableQty == shippingListAll.First().OutQty)
                    //    {
                    //        tran.Commit();
                    //        return true;
                    //    }
                    //    else
                    //    {
                    //        msg = "库存数量不满足出库数量";
                    //        tran.Rollback();
                    //        return false;
                    //    }
                    //}
                    //else
                    //{
                    //    List<TShippingOrderDetail> shippingList = new List<TShippingOrderDetail>();
                    //    foreach (var shipping in shippingListAll)
                    //    {
                    //        var shippingDetails = Context.TShippingOrderDetails
                    //            .Where(x => x.OrderSn == shipping.OrdeSn && x.Status != 4)
                    //            .ToList();
                    //        if (shippingDetails.Count() <= 0)
                    //        {
                    //            continue;
                    //        }
                    //        shippingList.AddRange(shippingDetails);
                    //    }
                    //    //遍历出库单信息并校验
                    //    foreach (var shipping in shippingList)
                    //    {
                    //        var InventoryList = Context.TInventories.Where(x => x.MaterialCode == shipping.MaterialCode).ToList();
                    //        if (InventoryList.Count() <= 0)
                    //        {
                    //            msg = "未查询到此物料";
                    //            tran.Rollback();
                    //            return false;
                    //        }
                    //        if (InventoryList.First().AvailableQty > shipping.Qty)
                    //        {
                    //            var cell = Context.TCells.Where(x => x.Code == palletBarAll.First().LocationCode).FirstOrDefault();
                    //            //回流任务添加上架单数据
                    //            TPutawayOrder putawayOrder = new TPutawayOrder()
                    //            {
                    //                Code = shipping.OrderSn,
                    //                //RelatedOrderDetailId = orderDetail.Id,
                    //                //StackingStorageId = stackingStorageId,
                    //                PalletBarcode = palletBarAll.First().PalletBarcode,
                    //                //ParentPalletBarcode = stackingStorage.ParentPalletBarcode,
                    //                LocationCode = cell.Code,
                    //                BillType = 6,
                    //                LocationDepth = cell.DepthIndex,
                    //                Status = 4,
                    //                Qty = palletBarcodeInInventory.First().AvailableQty - shippingListAll.First().OutQty,
                    //                CreateDate = DateTime.Now,
                    //                MaterialId = shippingOrderAll.MaterialId,
                    //                MaterialCode = shippingOrderAll.MaterialCode,
                    //                MaterialName = shippingOrderAll.MaterialName,
                    //                //MaterialKeyId = orderDetail.MaterialKeyId,
                    //                BatchNo = shippingOrderAll.BatchNo,
                    //                SmallBatchNo = shippingOrderAll.SmallBatchNo,
                    //                OwnerId = shippingOrderAll.OwnerId,
                    //                OwnerName = shippingOrderAll.OwnerName,
                    //                CreateUserId = 0,
                    //                CreateUserName = "系统",
                    //                SourceType = 1,
                    //                //站点编码
                    //                SiteCode = stations.First().Code,
                    //                //巷道
                    //                Roadway = cell.RoutewayId.ToString(),
                    //            };
                    //            Context.TPutawayOrders.Add(putawayOrder);
                    //            ////回流调动堆垛机
                    //            //TaskLiku task = new TaskLiku()
                    //            //{
                    //            //    taskCode = shipping.OrderSn,
                    //            //    taskType = "1",
                    //            //    startPoint = stations.First().Code,
                    //            //    endPoint = palletBarAll.First().LocationCode,
                    //            //};
                    //            //HttpHelper.WCSSendPostRequest("Api/ApiWCS/ReceiveTask", task);

                    //            //创建回流入库单
                    //            TReceivingOrderDetail receivingOrderDetail = new TReceivingOrderDetail();
                    //            DateTime now = DateTime.Now;
                    //            string year = now.Year.ToString();
                    //            string month = now.Month.ToString().PadLeft(2, '0');
                    //            string day = now.Day.ToString().PadLeft(2, '0');
                    //            string hour = now.Hour.ToString().PadLeft(2, '0');
                    //            string minute = now.Minute.ToString().PadLeft(2, '0');
                    //            string second = now.Second.ToString().PadLeft(2, '0');
                    //            receivingOrderDetail.OrderSn = "R" + $"{year}{month}{day}{hour}{minute}{second}";
                    //            receivingOrderDetail.BillType = 6;
                    //            receivingOrderDetail.WarehouseCode = shippingOrderAll.WarehouseCode;
                    //            var warehouseList = Context.TWarehouses.Where(x => x.Code == shippingOrderAll.WarehouseCode);
                    //            if (warehouseList.Count() <= 0)
                    //            {
                    //                msg = "未查询到此库房";
                    //                tran.Rollback();
                    //                return false;
                    //            }
                    //            receivingOrderDetail.WarehouseName = warehouseList.First().Name;
                    //            receivingOrderDetail.MaterialId = shippingOrderAll.MaterialId;
                    //            receivingOrderDetail.MaterialCode = shippingOrderAll.MaterialCode;
                    //            receivingOrderDetail.MaterialName = shippingOrderAll.MaterialName;
                    //            receivingOrderDetail.MaterialTypeCode = shippingOrderAll.MaterialTypeCode;
                    //            receivingOrderDetail.AreaCode = shippingOrderAll.AreaCode;
                    //            receivingOrderDetail.AreaName = shippingOrderAll.AreaName;
                    //            receivingOrderDetail.PalletBarcode = PalletBarcode;
                    //            receivingOrderDetail.Qty = palletBarcodeInInventory.First().AvailableQty - shippingListAll.First().OutQty;
                    //            receivingOrderDetail.ReceivedQty = palletBarcodeInInventory.First().AvailableQty - shippingListAll.First().OutQty;
                    //            receivingOrderDetail.UnitId = shippingOrderAll.UnitId;
                    //            receivingOrderDetail.UnitName = shippingOrderAll.UnitName;
                    //            receivingOrderDetail.Erpcode = shippingOrderAll.Erpcode;
                    //            receivingOrderDetail.BatchNo = shippingOrderAll.BatchNo;
                    //            receivingOrderDetail.SmallBatchNo = shippingOrderAll.SmallBatchNo;
                    //            receivingOrderDetail.OwnerId = shippingOrderAll.OwnerId;
                    //            receivingOrderDetail.OwnerName = shippingOrderAll.OwnerName;
                    //            receivingOrderDetail.Status = 3;
                    //            receivingOrderDetail.LocationId = palletBarcodeInInventory.First().LocationId;
                    //            receivingOrderDetail.LocationName = palletBarcodeInInventory.First().LocationName;
                    //            receivingOrderDetail.LocationCode = palletBarcodeInInventory.First().LocationCode;
                    //            receivingOrderDetail.CreateDate = DateTime.Now;
                    //            receivingOrderDetail.MaterialName = shippingOrderAll.MaterialName;
                    //            receivingOrderDetail.Status = 2;
                    //            Context.TReceivingOrderDetails.Add(receivingOrderDetail);

                    //            decimal? returnQty = palletBarcodeInInventory.First().AvailableQty - shippingListAll.First().OutQty;
                    //            msg = "物料:" + shippingListAll.First().MaterialName + "回流" + returnQty;
                    //        }
                    //        else if (InventoryList.First().AvailableQty < shipping.Qty)
                    //        {
                    //            msg = "库存数量不满足出库数量";
                    //            tran.Rollback();
                    //            return false;
                    //        }
                    //        else
                    //        {
                    //            continue;
                    //        }

                    //    }
                    //}
                    #endregion
                    tran.Commit();
                    return true;
                }
                catch
                {
                    msg = "发生错误";
                    tran.Rollback();
                    return false;
                }

        }

        /// <summary>
        /// 拣选入库
        /// </summary>
        /// <param name="PalletBarcode"></param>
        /// <param name="Username"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool GetSelectPutShipping(string PalletBarcode, string code, string MaterialName, int number, out string msg)
        {
            using (var tran = Context.Database.BeginTransaction())
            {
                try
                {
                    msg = "";
                    //查询是否有此托盘
                    var palletBarAll = Context.TPallets.Where(x => x.PalletBarcode == PalletBarcode).ToList();
                    if (palletBarAll.Count() <= 0)
                    {
                        msg = "未查询到此托盘";
                        tran.Rollback();
                        return false;
                    }
                    //根据托盘查找库存数据
                    var palletBarcodeInInventorys = Context.TInventories.Where(x => x.PalletBarcode == PalletBarcode).FirstOrDefault();
                    if (palletBarcodeInInventorys == null)
                    {
                        msg = "未找到库存数据";
                        tran.Rollback();
                        return false;
                    }
                    else
                    {
                        //同物料数量相加
                        if (palletBarcodeInInventorys.MaterialName == MaterialName)
                        {
                            palletBarcodeInInventorys.AvailableQty = palletBarcodeInInventorys.AvailableQty + number;
                        }
                        Context.TInventories.Update(palletBarcodeInInventorys);
                        if (Context.SaveChanges() <= 0)
                        {
                            msg = "库存修改失败";
                            tran.Rollback();
                            return false;
                        }
                        DateTime now = DateTime.Now;
                        string year = now.Year.ToString();
                        string month = now.Month.ToString().PadLeft(2, '0');
                        string day = now.Day.ToString().PadLeft(2, '0');
                        string hour = now.Hour.ToString().PadLeft(2, '0');
                        string minute = now.Minute.ToString().PadLeft(2, '0');
                        string second = now.Second.ToString().PadLeft(2, '0');
                        string millsecond = now.Millisecond.ToString();
                        //var bind = Context.BindPickingPositionModel.Where(x => x.NowLoginUserName == Username).FirstOrDefault();
                        var cell = Context.TCells.Where(x => x.Code == palletBarAll.First().LocationCode).FirstOrDefault();
                        //回流任务添加上架单数据
                        TPutawayOrder putawayOrder = new TPutawayOrder()
                        {
                            Code = "PUT" + $"{year}{month}{day}{hour}{minute}{second}{millsecond}",
                            //RelatedOrderDetailId = orderDetail.Id,
                            //StackingStorageId = stackingStorageId,
                            PalletBarcode = palletBarAll.First().PalletBarcode,
                            //ParentPalletBarcode = stackingStorage.ParentPalletBarcode,
                            LocationCode = cell.Code,
                            BillType = 6,
                            LocationDepth = cell.DepthIndex,
                            Status = 4,
                            Qty = palletBarcodeInInventorys.AvailableQty,
                            CreateDate = DateTime.Now,
                            MaterialId = palletBarcodeInInventorys.MaterialId,
                            MaterialCode = palletBarcodeInInventorys.MaterialCode,
                            MaterialName = palletBarcodeInInventorys.MaterialName,
                            //MaterialKeyId = orderDetail.MaterialKeyId,
                            BatchNo = palletBarcodeInInventorys.BatchNo,
                            SmallBatchNo = palletBarcodeInInventorys.SmallBatchNo,
                            OwnerId = palletBarcodeInInventorys.OwnerId,
                            OwnerName = palletBarcodeInInventorys.OwnerName,
                            CreateUserId = 0,
                            CreateUserName = "系统",
                            SourceType = 1,
                            //站点编码
                            SiteCode = code,
                            //巷道
                            Roadway = cell.RoutewayId.ToString(),
                        };
                        Context.TPutawayOrders.Add(putawayOrder);
                        if (Context.SaveChanges() <= 0)
                        {
                            msg = "上架单保存失败";
                            tran.Rollback();
                            return false;
                        }
                        //回流入库下发任务
                        //PutChooseDto dto = new PutChooseDto()
                        //{
                        //    taskCode = putawayOrder.Code,
                        //    SiteCode = putawayOrder.SiteCode,
                        //    Roadway = putawayOrder.Roadway
                        //};
                        //HttpHelper.SendPostRequest("入库拣选", dto);
                    }
                    tran.Commit();
                    return true;
                }
                catch
                {
                    msg = "发生错误";
                    tran.Rollback();
                    return false;
                }
            }
        }
        /// <summary>
        /// 根据托盘码查询
        /// </summary>
        /// <param name="palletbarcode"></param>
        /// <returns></returns>
        public List<TInventory> GetSelectPutTinven(string palletbarcode)
        {
            //var putobj = Context.TPutawayOrders.FirstOrDefault(x => x.PalletBarcode == palletbarcode && x.Status == 4 && x.BillType == 6);
            //if (putobj != null)
            //{
            //    return new List<TInventory>();
            //}
            //上架单
            var invens = Context.TInventories.Where(x => x.PalletBarcode == palletbarcode);
           
            return invens.ToList();
        }

        /// <summary>
        /// 获取托盘满载率
        /// </summary>
        /// <param name="PalletBarcode"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public decimal? GetPallLoadFactor(string PalletBarcode, out string msg)
        {
            msg = "";
            //获取托盘信息
            var palletList = Context.TPallets.Where(x => x.PalletBarcode == PalletBarcode).ToList();
            if(palletList.Count <= 0)
            {
                msg = "暂无此托盘";
                return 0;
            }
            //托盘总负荷
            decimal? palletWeight = palletList.First().MaximumWeightOfTray;       
            //获取出库单信息
            var shippingList = Context.TShippingLockCells.Where(x => x.PalletBarcode == PalletBarcode).ToList();
            if(shippingList.Count() <= 0)
            {
                msg = "该托盘未绑定出库单";
                return 0;
            }
            //物料重量
            decimal? materialGrossWeight = 0;
            //物料数量
            decimal? materialQty = 0;
            //物料总重量
            decimal? materialWeightSum = 0;
            decimal? num = 0;
            decimal? count = 0;
            foreach ( var shipping in shippingList )
            {
                var shippingOrderList = Context.TShippingOrderDetails.Where(x => x.OrderSn == shipping.OrdeSn).ToList();
                if(shippingOrderList.Count() <= 0)
                {
                    msg = "暂无出库单信息";
                    return 0;
                }
                
                foreach (var shippingOrder in shippingOrderList)
                {
                    if(shippingOrder.Status != 4 && shipping.OrdeSn == shippingOrder.OrderSn)
                    {
                        //获取物料
                        var materialList = Context.TMaterial.Where(x => x.Code == shippingOrder.MaterialCode).ToList();
                        if(materialList.Count() <= 0)
                        {
                            msg = "暂无此物料:" + shippingOrder.MaterialCode;
                            return 0;
                        }
                        //物料重量
                        materialGrossWeight = materialList.First().GrossWeight;
                        //物料数量
                        materialQty = shippingOrder.Qty;
                        //物料总重量
                        materialWeightSum = materialGrossWeight * materialQty;
                        num += materialWeightSum;
                      
                    }
                }
            }
            count = num / palletWeight;

            return count * 100;
        }

        /// <summary>
        /// 根据托盘条码带出相关信息
        /// </summary>
        /// <param name="PalletBarcode"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public StackingReceivingsDto GetInformationByBarcode(string PalletBarcode, out string msg)
        {
            msg = "";
            //var stackingStoragelist = Context.TStackingStorages.Where(x => x.Status == 1 && x.PalletBarcode == PalletBarcode);

            //if (!stackingStoragelist.Any())
            //{
            //    msg = "无组垛信息";
            //    return null;
            //}

            //查看上架单
            var putobj = Context.TPutawayOrders.FirstOrDefault(x => x.PalletBarcode == PalletBarcode && x.BillType == 6 && x.Status == 4);
            if (putobj != null) 
            {
                msg = "该托盘正在拣选作业中";
                return null;
            }
            var invobjlist = Context.TInventory.Where(x => x.Status == 4 && x.PalletBarcode == PalletBarcode);
            if (!invobjlist.Any())
            {
                msg = "无库存信息";
                return null;
            }
            StackingReceivingsDto srmodel = new StackingReceivingsDto();
            foreach (var item in invobjlist)
            {
                    srmodel.PallteCode = PalletBarcode;
                    srmodel.Cell = item.LocationCode;
                    MaterNum maternum = new MaterNum();
                    maternum.MaterialCode = item.MaterialCode;
                    maternum.Num = item.AvailableQty;
                    srmodel.materNum.Add(maternum);
            }
            return srmodel;
        }
    }
}
